package com.simple.business.service.utilService.impl;

import com.github.zuihou.base.R;
import com.simple.business.service.utilService.LLMService;
import com.simple.llm.domain.dto.LLMMessageDTO;
import com.simple.llm.domain.dto.NormalChatDTO;
import com.simple.llm.domain.vo.LLMResponseVO;
import com.simple.llm.service.LLMContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class LLMServiceImpl implements LLMService {
    @Resource
    private LLMContext llmContext;

    @Override
    public LLMResponseVO chat(List<LLMMessageDTO> list, Float temperature, Float topP, String desc, Long userId, String llmName) {
        try {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setIndex(i);
            }
            NormalChatDTO chatDTO = NormalChatDTO.builder()
                    .createUser(StringUtils.isBlank(String.valueOf(userId)) ? "0" : String.valueOf(userId))
                    .temperature(temperature)
                    .topP(topP)
                    .desc(desc)
                    .messages(list)
                    .modelName(llmName)
                    .build();
            R<LLMResponseVO> result = llmContext.chat(chatDTO);
            return result.getData();
        }catch (Exception exception){
            log.error("默认chat失败",exception);
        }
        return null;
    }
}
